from selenium import selenium
import unittest, time, re, xmlrunner, os, base
from ConfigParser import SafeConfigParser 

#Variable Definitions
parser = SafeConfigParser()
path_to_project = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
parser.read(path_to_project + '/config/Config.ini')
url = parser.get('SETTINGS','URL')
br = parser.get('SETTINGS','BROWSER')
server = parser.get('SETTINGS','server')
#delay = parser.get('SETTINGS','delay')


class Demo(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.selenium = selenium(server, 4444, br, url)
        self.selenium.start() 
        self.selenium.set_timeout("60000")
        print("setUpClass")       
        self.selenium.open("/")
        self.selenium.window_maximize()
    def setUp(self):
        self.verificationErrors = []

    
    def test_demo_data_driven(self):
        """Loops through a list of users for registration page """
        print("opening homepage")
        sel = self.selenium
        #sel.window_maximize()
        #sel.open("/")
        print("register dd")
        sel = self.selenium
        sel.window_maximize()
        sel.delete_all_visible_cookies()
        sel.open("/")      
        
        ## Data driven account registration test (Working)   
        accounts = base.get_csv_data(path_to_project + '/config/register_account.csv')
        #print(accounts)
        i = 0
        while False:#i < len(accounts):
            time.sleep(1)
            sel.type("css=input[id='id_email']", accounts[i])           
            i=i+1
            sel.type("css=input[id='id_email2']", accounts[i])          
            i=i+1
            sel.type("css=form[id='register'] input[id='id_password']", accounts[i])         
            i=i+1
            time.sleep(1)
            sel.click("css=form[id=register] > input.submit") ## E > F    Matches any F element that is a child of an element E.
            sel.wait_for_page_to_load("30000")
            i=i+1
            time.sleep(1) 
            
        expectedText = "Sign Upzzz"
        actualText = sel.get_value("css=#register > input.submit")
        buttonName="Sign up"
        try: self.assertEqual(expectedText, actualText,"(%s) button text is incorrect (%s); expected(%s)"%(buttonName,actualText,expectedText))
        except AssertionError, e: self.verificationErrors.append(str(e))  
         
        try: self.assertEqual("Sign Up", sel.get_value("css=#register > input.submit"))
        except AssertionError, e: self.verificationErrors.append(str(e))    
              
        try: self.assertEqual(expectedText, actualText,"(%s) button text is incorrect (%s); expected(%s)"%(buttonName,actualText,expectedText))
        except AssertionError, e: self.verificationErrors.append(str(e))   



#            try: self.failUnless(sel.is_text_present( accounts[i]),"Element not  found: %s"%(accounts[i]))
#            except AssertionError, e: self.verificationErrors.append(str(e))      
                
                
    
    
    
    def tearDown(self):
        #self.selenium.stop()
        self.assertEqual([], self.verificationErrors,"Results: " + str(self.verificationErrors))
    @classmethod    
    def tearDownClass(self):
        self.selenium.stop()
        print("tearDownClass")

if __name__ == "__main__":
    #unittest.main()
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))

